package cn.lkk.samples.lang.classes.ObjectConstruction;

import java.util.Random;

/**
 * 本程序演示对象的构造
 */
public class ConstructorTest {
    public static void main(String[] args) {

        //填充一个Employee数组
        Employee[] staff = new Employee[3];
        staff[0] = new Employee("Harry", 4000);
        staff[1] = new Employee(6000);
        staff[2] = new Employee();

        for (Employee e:staff)
            System.out.println("name="+e.getName()+",id="+e.getId()+",salary="+e.getSalary());

    }
}

class Employee{
    private static int nextId;
    //静态初始化块
    static {
        Random generator = new Random();
        nextId = generator.nextInt(10000);
    }

    private int id; //1 默认初始化
    private String name = ""; //2 实例字段初始化
    private double salary;


    //3. 对象初始化块
    {
        id = nextId;
        nextId++;
    }

    //4. 构造方法
    public Employee(String n,double s){
        name=n;
        salary = s;
    }

    public Employee(double s) {
        //调用其他构造函数
        this("Employee#" + nextId, s);
    }

    //默认构造函数
    public Employee(){

    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }
}
